#include"Logger.h"
#include<time.h>
#include<iostream>
Logger & Logger ::GetInstance()
{
  static Logger logger;
  return logger;
}
void Logger::SetLogLevel(LOG_LEVEL logLevel)
{
 m_logLevel = logLevel;
}
void Logger::Log(std::string msg)
{
  m_lockQue.Push(msg);
}

Logger::Logger()
{
  std::thread writeLog([&](){
    for(;;)
    {
        time_t now = time(nullptr);
        tm * nowTime = localtime(&now);
        char fileName [1024]={0};
        sprintf(fileName,"%d-%d-%d",nowTime->tm_year+1900,nowTime->tm_mon+1,nowTime->tm_mday);
        FILE * fp = fopen(fileName,"a+");
        if(nullptr == fp)
        {
            std::cout<<"Log file error"<<std::endl;
            exit(-1);
            std::string msg = m_lockQue.Pop();
            char timeBuff[1024]={0};
            sprintf(timeBuff,"%d-%d-%d",nowTime->tm_hour,nowTime->tm_min,nowTime->tm_sec);
            msg.insert(0,timeBuff);
            if(msg.size() == 0)
            {
                fclose(fp);
            }
            fputs(msg.c_str(),fp);
            
        }

    }
  });
   writeLog.detach();
}